﻿using System;
using Inovout.Business;
using System.Windows;
using Inovout.Gui.Wpf.ViewModels;
using Inovout.Gui.Wpf.Views;
using Inovout.Portal.Desktop.Workbenchs;

namespace Inovout.Portal.Desktop
{
    public class GuiAction : IGuiAction
    {
        private MainWorkbenchViewModel mainWorkbenchViewModel;
        public GuiAction()
        {
            this.mainWorkbenchViewModel = Application.Current.MainWindow.DataContext as MainWorkbenchViewModel;
        }
        private DomainViewModel CreateViewModel(WorkItemContext workItemContext)
        {
            DomainViewModel viewModel = null;
            if (workItemContext.ViewData.Model.GetType().IsGenericType)
            {
                Type modelType = workItemContext.ViewData.Model.GetType().GetGenericArguments()[0];
                viewModel = ComponentRegistry.Resolve<DomainViewModel>(modelType.Name.ToLower() + "CollectionViewModel".ToLower());
            }
            else
            {
                viewModel = ComponentRegistry.Resolve<ModelViewModel>(workItemContext.ViewData.Model.GetType().Name.ToLower() + "ViewModel".ToLower());
            }
            viewModel.Init(new ViewContext { WorkItemContext = workItemContext });
            return viewModel;
        }
        private IView CreateView(WorkItemContext workItemContext)
        {
            return ComponentRegistry.Resolve<IView>(workItemContext.ViewName.ToLower() + "View".ToLower());
        }
        public void View(WorkItemContext workItemContext)
        {
            DomainViewModel viewModel = CreateViewModel(workItemContext);
            mainWorkbenchViewModel.AddToDocument(viewModel);
        }


        public void Dialog(WorkItemContext workItemContext)
        {
            DomainViewModel viewModel = CreateViewModel(workItemContext);
            IDialogWorkbench dialogWorkbench = ComponentRegistry.Resolve<IDialogWorkbench>(WpfApplication.DialogWorkbenchKey);
            MainWorkbenchViewModel dialogWorkbenchViewModel = ((Window)dialogWorkbench).DataContext as MainWorkbenchViewModel;
            dialogWorkbenchViewModel.Documents.Add(viewModel);
            dialogWorkbench.ShowDialog(true);
        }

        public void Message(string msg)
        {
            throw new NotImplementedException();
        }

        //internal void View(WorkItemContext workItemContext)
        //{
        //    SmartPartViewModel viewModel;
        //    if (!ComponentManager.TryResolve<SmartPartViewModel>(workItemContext.ViewName.ToLower() + "viewmodel", out viewModel))
        //    {
        //        Type modelType = workItemContext.ViewData.Model.GetType();
        //        string viewModelKey = modelType.Name;
        //        if (modelType.IsGenericType)
        //        {
        //            modelType = modelType.GetGenericArguments()[0];
        //            viewModelKey = modelType.Name + "collection";
        //        }
        //        viewModel = ComponentManager.Resolve<SmartPartViewModel>(viewModelKey.ToLower() + "viewmodel") as SmartPartViewModel;
        //    }
        //    viewModel.WorkItemContext = workItemContext;
        //    if (viewModel is DocumentViewModel)
        //    {
        //        this.documents.Add(viewModel as DocumentViewModel);
        //    }
        //    else
        //    {
        //        this.pads.Add(viewModel as PadViewModel);
        //    }
        //}

        //internal void Dialog(WorkItemContext workItemContext)
        //{

        //}
        //private IViewContent CreateViewContent(WorkItemContext workItemContext)
        //{
        //    return null;
        //    //    IViewContent viewContent = ComponentManager.Resolve<IViewContent>(workItemContext.ViewName.ToLower() + "view");
        //    //    Type modelType =workItemContext.ViewData.Model.GetType();
        //    //    string viewModelKey = modelType.Name;
        //    //    if(modelType.IsGenericType)
        //    //    {
        //    //        modelType = modelType.GetGenericArguments()[0];
        //    //        viewModelKey = modelType.Name + "collection";
        //    //    }
        //    //    BaseViewModel viewModel = ComponentManager.Resolve<BaseViewModel>(viewModelKey.ToLower() + "viewmodel");
        //    //    viewModel.ViewData = workItemContext.ViewData;
        //    //    ((BaseView)viewContent).DataContext = viewModel;
        //    //    return viewContent;
        //}

        //internal void Message(string message)
        //{
        //    //     MessageBox.Show(message);

        //}
    }
}
